package com.lfsenior.listmq;

import redis.clients.jedis.Jedis;

import java.util.Random;

/**
 * Created by dlupan on 2018/10/15
 */
public class TaskConsumer implements Runnable{
    Jedis jedis=new Jedis("127.0.0.1",6379);

    @Override
    public void run() {
        Random random=new Random();
        while (true){
            //从任务队列"task-queue"中获取一个任务，并将该任务放入暂存队列"tmp-queue
            String taskId = jedis.rpoplpush("task-queue", "tmp-queue");

            //处理了任务----纯属业务逻辑
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //模拟成功和失败
            if(random.nextInt(13)%7==0){
                jedis.rpoplpush("tmp-queue","task-queue");
                System.out.println(taskId+"处理失败，弹回任务队列");
            }else{
                jedis.rpop("tmp-queue");
                System.out.println(taskId+"处理成功，被清除");
            }
        }
    }
}
